package com.woniuxy.service.impl;

import com.woniuxy.Exception.serviceException;
import com.woniuxy.dao.DoctorDao;
import com.woniuxy.entity.Doctor;
import com.woniuxy.entity.DoctorAndDepartment;
import com.woniuxy.entity.Equipment;
import com.woniuxy.entity.PageBean;
import com.woniuxy.service.DoctorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    DoctorDao doctorDao;

    @Override
    public void addDoctor(Doctor doctor) {
        doctorDao.insertDoctor(doctor);
        log.info("添加成功");
    }

    @Override
    public void removeDoctor(int doctorId) {
        doctorDao.deleteByDoctorId(doctorId);
        log.info("删除成功");
    }

    @Override
    public void updateDoctor(Doctor doctor) {
        doctorDao.updateDoctor(doctor);
    }

    @Override
    public PageBean<Doctor> getAllDoctors(Doctor doctor, int pageIndex, int pageSize) {

        try {
            PageBean<Doctor> pageBean = new PageBean<>();
            //存储当前页
            pageBean.setPageIndex(pageIndex);
            //存储页大小
            pageBean.setPageSize(pageSize);
            //获取总条数
            int pageCount = doctorDao.selectDoctorCount(doctor);
            //存储总条数
            pageBean.setTotalRecord(pageCount);

            pageBean.setBeanList(doctorDao.selectDoctor(doctor, pageIndex, pageSize));
            return pageBean;
        } catch (PersistenceException e) {
            throw new serviceException("系统维护中");
        }
    }

    @Override
    public DoctorAndDepartment selectDoctorMessageById(int doctorId) {
        return doctorDao.selectDoctorMessageById(doctorId);
    }

    @Override
    public List<Doctor> selectByDepartments(String departmentsName) {
        return doctorDao.selectByDepartments(departmentsName);
    }

    @Override
    public Doctor selectDoctorByName(String doctorName) {
        return doctorDao.selectDoctorByName(doctorName);
    }

}
